import axios from 'axios'
// 全局配置文件
import commconfig from '@/comm.json'
import sessionStorage from '@/api/Storage'
import 'url-search-params-polyfill'
// 角色功能权限控制
export function Jurisdiction(moduleActionName, thiobj) {
  var list = JSON.parse(sessionStorage.getItem('userActionList'))
  var istrue = false
  var rotes = thiobj.$router.options.routes
  var path = thiobj.$router.currentRoute.path
  var moduleid = ''
  rotes.forEach(r => {
    r.children.forEach(c => {
      if (c.path == path) {
        moduleid = c.params.menuid
        return
      }
    })
  })
  list.forEach(element => {
    element.moduleDtos.forEach(module => {
      if (
        module.moduleActionName == moduleActionName &&
        module.moduleID == moduleid
      ) {
        istrue = true
      }
    })
  })
  return istrue
}
// Ajax 普通请求
export function Ajax(thiobj, _url, _parm, method, callBack, errorcallBack) {
  axios({
    url: commconfig.baseapi + _url,
    method: method,
    data: _parm,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    }
  })
    .then(respanse => {
      callBack(respanse)
    })
    .catch(function (error) {
      errorcallBack(error)
    })
}
// Ajax 上传文件
export function AjaxUpload(thiobj, _url, _parm, method, callBack, errorcallBack) {
  axios({
    url: commconfig.baseapi + _url,
    method: method,
    data: _parm,
    headers: {
      "Content-Type": "multipart/form-data;boundary=" + new Date().getTime(),
    }
  })
    .then(respanse => {
      callBack(respanse)
    })
    .catch(function (error) {
      errorcallBack(error)
    })
}

    /**
     * 自定义上传文件
     * @param fileList 文件列表
     * @param data 上传时附带的额外参数
     * @param url 上传的URL地址
     * @param success 成功回调
     * @param error 失败回调
     */



// Ajax Toke验证请求
export function AjaxToken(thiobj, _url, _parm, method, callBack, errorcallBack) {
  axios({
    url: commconfig.baseapi + _url,
    method: method,
    data: _parm,

    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
      'Authorization': sessionStorage.getItem('token')
    }
  }).then(respanse => {
    if (respanse.data.ErrorCode == 401 || respanse.data.ErrorCode == 402 || respanse.data.ErrorCode == 204) {
      thiobj.$message({
        type: 'error',
        message: respanse.data.Message
      })
      sessionStorage.clear()
      thiobj.$router.push(`/login?redirect=dashboard`)
    } else if (respanse.data != undefined) {
      callBack(respanse)
    } else if (respanse.data == '登录已过期,请退出重新登录') {
      next({
        path: '/login',
        query: {
          msg: '登录已过期,请退出重新登录'
        }
      })
    } else {
      next({
        path: '/login',
        query: {
          msg: respanse.data
        }
      })
    }
  })
    .catch(function (error) {
      errorcallBack(error)
    })
}


export function AjaxTokenNotasync(thiobj, _url, _parm, method, callBack, errorcallBack) {
  axios({
    url: commconfig.baseapi + _url,
    method: method,
    data: _parm,
    async: false, // fasle表示同步请求，true表示异步请求
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
      'Authorization': sessionStorage.getItem('token')
    }
  }).then(respanse => {
    if (respanse.data.ErrorCode == 401 || respanse.data.ErrorCode == 402 || respanse.data.ErrorCode == 204) {
      thiobj.$message({
        type: 'error',
        message: respanse.data.Message
      })
      sessionStorage.clear()
      thiobj.$router.push(`/login?redirect=dashboard`)
    } else if (respanse.data != undefined) {
      callBack(respanse)
    } else if (respanse.data == '登录已过期,请退出重新登录') {
      next({
        path: '/login',
        query: {
          msg: '登录已过期,请退出重新登录'
        }
      })
    } else {
      next({
        path: '/login',
        query: {
          msg: respanse.data
        }
      })
    }
  })
    .catch(function (error) {
      errorcallBack(error)
    })
}
// Ajax get请求
export function AjaxGet(thiobj, _url, callBack, errorcallBack) {
  axios({
    url: commconfig.baseapi + _url,
    method: 'get',
    headers: {
      'Authorization': sessionStorage.getItem('token')
    }
  })
    .then(respanse => {
      if (respanse.data.ErrorCode == 401 || respanse.data.ErrorCode == 402) {
        thiobj.$message({
          type: 'error',
          message: data.Message
        })
        sessionStorage.clear()
        thiobj.$router.push(`/login?redirect=dashboard`)
      } else if (respanse.data != undefined) {
        callBack(respanse)
      } else if (respanse.data == '登录已过期,请退出重新登录') {
        next({
          path: '/login',
          query: {
            msg: '登录已过期,请退出重新登录'
          }
        })
      } else {
        next({
          path: '/login',
          query: {
            msg: respanse.data
          }
        })
      }
    })
    .catch(function (error) {
      errorcallBack(error)
    })
}
// 下拉列表普通请求
export function SelectAjax(_url, callBack, errorcallBack) {
  axios({
    url: commconfig.baseapi + _url,
    method: 'get',
    data: [],
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
      'Authorization': sessionStorage.getItem('token')
    }
  })
    .then(respanse => {
      callBack(respanse)
    })
    .catch(function (error) {
      errorcallBack(error)
    })
}
export function TipMsg(thisobj, message) {
  thisobj.$message({
    showClose: true,
    message: message,
    type: 'error'
  })
  return
}
export function TipConfirm(thiobj, message, succescallBack, cnacelcallBack) {
  thiobj.$confirm(message, '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'error'
  }).then(() => {
    succescallBack(thiobj)
  })
    .catch(() => {
      thiobj.$message({
        type: 'info',
        message: '已取消删除'
      })
    })
}

export function SetcachedViews(thisobj) {
  var rotes = thisobj.$router.options.routes// 目前页面是全部缓存,后期通过后台进行控制,单页缓存
  var viewnames = []
  rotes.forEach(r => {
    if (r.meta != undefined && r.meta.keepAlive) {
      var urls = r.path.split('/')
      if (urls != undefined) {
        var viewname = urls[urls.length - 1]
        if (urls.length > 2) {
          viewnames.push(viewname)
        }
      }
    }
    if (r.children != undefined) {
      r.children.forEach(c => {
        if (c.meta != undefined && c.meta.keepAlive) {
          var urls = c.path.split('/')
          if (urls != undefined) {
            var viewname = urls[urls.length - 1]
            if (urls.length > 2) {
              viewnames.push(viewname)
            }
          }
        }
      })
    }
  })
  viewnames.push('index')
  return viewnames
}